using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Configuration;
using Fawcett.Plugins;
using System.Timers;
using System.Threading;
using System.Collections.Generic;
using System.Diagnostics;

namespace Fawcett.PluggableApp
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class MainForm : Form
    {
       
        private IContainer components = null;
        private Ribbon ribbon1;
        private RibbonTab HomeTab;
        private RibbonPanel AddonPanel;
        private RibbonButton cmdList;
        private StatusStrip statusStrip1;
        private static System.Timers.Timer aTimer;
        private ToolStripStatusLabel toolStripStatusLabel1;
        private RibbonTab SystemTab;

        //The collection of currently active plugins
        private PluginCollection m_plugins = null;

        public MainForm()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            aTimer = new System.Timers.Timer(2000);
            aTimer.Elapsed += OnTimedEvent;
            aTimer.Enabled = true;
            //

            GlobalEvent.MyEvent += PluginEventHandler;
        }

        public void PluginEventHandler(object sender, EPlugin p)
        {

            if (p.GetCode == PluginCode.ERROR)
            {
                SetStatusError( p.GetInfo );
            }
        }

        private void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
          //  GlobalEvent.FireEvent(this, new EPlugin(PluginCode.MESSAGE, "The Elapsed event was raised at " + e.SignalTime));
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
            this.ribbon1 = new System.Windows.Forms.Ribbon();
            this.HomeTab = new System.Windows.Forms.RibbonTab();
            this.AddonPanel = new System.Windows.Forms.RibbonPanel();
            this.cmdList = new System.Windows.Forms.RibbonButton();
            this.SystemTab = new System.Windows.Forms.RibbonTab();
            this.statusStrip1 = new System.Windows.Forms.StatusStrip();
            this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
            this.statusStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // ribbon1
            // 
            this.ribbon1.Font = new System.Drawing.Font("Segoe UI", 9F);
            this.ribbon1.Location = new System.Drawing.Point(0, 0);
            this.ribbon1.Minimized = false;
            this.ribbon1.Name = "ribbon1";
            // 
            // 
            // 
            this.ribbon1.OrbDropDown.BorderRoundness = 8;
            this.ribbon1.OrbDropDown.Location = new System.Drawing.Point(0, 0);
            this.ribbon1.OrbDropDown.Name = "";
            this.ribbon1.OrbDropDown.Size = new System.Drawing.Size(527, 72);
            this.ribbon1.OrbDropDown.TabIndex = 0;
            this.ribbon1.OrbImage = null;
            this.ribbon1.OrbStyle = System.Windows.Forms.RibbonOrbStyle.Office_2010;
            this.ribbon1.OrbVisible = false;
            this.ribbon1.RibbonTabFont = new System.Drawing.Font("Trebuchet MS", 9F);
            this.ribbon1.Size = new System.Drawing.Size(505, 140);
            this.ribbon1.TabIndex = 1;
            this.ribbon1.Tabs.Add(this.HomeTab);
            this.ribbon1.Tabs.Add(this.SystemTab);
            this.ribbon1.TabsMargin = new System.Windows.Forms.Padding(12, 26, 20, 0);
            this.ribbon1.Text = "ribbon1";
            this.ribbon1.ThemeColor = System.Windows.Forms.RibbonTheme.Blue;
            // 
            // HomeTab
            // 
            this.HomeTab.Panels.Add(this.AddonPanel);
            this.HomeTab.Text = "Home";
            // 
            // AddonPanel
            // 
            this.AddonPanel.ButtonMoreEnabled = false;
            this.AddonPanel.ButtonMoreVisible = false;
            this.AddonPanel.Items.Add(this.cmdList);
            this.AddonPanel.Text = "Add Ons";
            // 
            // cmdList
            // 
            this.cmdList.DrawIconsBar = false;
            this.cmdList.DropDownResizable = true;
            this.cmdList.Image = ((System.Drawing.Image)(resources.GetObject("cmdList.Image")));
            this.cmdList.SmallImage = ((System.Drawing.Image)(resources.GetObject("cmdList.SmallImage")));
            this.cmdList.Text = "List";
            this.cmdList.Click += new System.EventHandler(this.cmdList_Click);
            // 
            // SystemTab
            // 
            this.SystemTab.Text = "Systems";
            // 
            // statusStrip1
            // 
            this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripStatusLabel1});
            this.statusStrip1.Location = new System.Drawing.Point(0, 622);
            this.statusStrip1.Name = "statusStrip1";
            this.statusStrip1.Size = new System.Drawing.Size(505, 22);
            this.statusStrip1.TabIndex = 3;
            this.statusStrip1.Text = "statusStrip1";
            // 
            // toolStripStatusLabel1
            // 
            this.toolStripStatusLabel1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
            this.toolStripStatusLabel1.RightToLeftAutoMirrorImage = true;
            this.toolStripStatusLabel1.Size = new System.Drawing.Size(66, 17);
            this.toolStripStatusLabel1.Text = "Status Strip";
            // 
            // MainForm
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(505, 644);
            this.Controls.Add(this.statusStrip1);
            this.Controls.Add(this.ribbon1);
            this.IsMdiContainer = true;
            this.Name = "MainForm";
            this.Text = "FSX Console";
            this.statusStrip1.ResumeLayout(false);
            this.statusStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// 
        public static  MainForm mf = new MainForm();
        /// </summary>
        [STAThread]
        static void Main()
        {

            Application.Run(  mf);
        }

        protected override void OnLoad(EventArgs e)
        {

            base.OnLoad(e);
            LoadPlugins();
        }


        private void LoadPlugins()
        {
            //Retrieve a plugin collection using our custom Configuration section handler
            m_plugins = (PluginCollection)ConfigurationManager.GetSection("plugins");

            foreach (IPlugin plugin in m_plugins)
            {

                Debug.WriteLine( String.Format( "Loading  plugin {0}", plugin.Name) );

                RibbonTab Temp = (plugin.Location == TabLocation.OWN ) ? null :
                    (plugin.Location == TabLocation.FAULT ) ? HomeTab :
                    (plugin.Location == TabLocation.HOME ) ? HomeTab :
                    (plugin.Location == TabLocation.SYSTEM ) ? SystemTab : null ;

                RibbonTab newRibbonTab = plugin.SetupMenu( Temp );
                if (newRibbonTab != null )
                {
                    this.ribbon1.Tabs.Add(newRibbonTab) ;    // Configure Menu
                    
                }
                GlobalEvent.MyEvent += plugin.PluginEventHandler;
                plugin.parent = this;
            }

        }



        private void SetStatusError(string text)
        {
            SetStatus(Color.Red, "ERROR " +  text);
        }

        private void SetStatusText(string text)
        {
            SetStatus(Color.Black, text);
        }
        delegate void SetTextCallback(Color col , string text);
        private void SetStatus(Color col,  string text)
        {
            // InvokeRequired required compares the thread ID of the 
            // calling thread to the thread ID of the creating thread. 
            // If these threads are different, it returns true. 
            if (this.statusStrip1.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetStatus);
                this.Invoke(d, new object[] { col, text });
            }
            else
            {
                toolStripStatusLabel1.Text =  text;
                toolStripStatusLabel1.ForeColor = col;
            }
        }
        private void toolStripStatusLabel1_Click(object sender, EventArgs e)
        {

        }

        private void cmdList_Click(object sender, EventArgs e)
        {
            ListForm<PluginCollection> nf = new ListForm<PluginCollection>("List of Plugins", m_plugins);

            if (nf.MdiParent == null) nf.MdiParent = this;

            nf.Show();

        }





    }
}